using System;
using System.Collections.Generic;
using System.Text;
using System.Collections.Specialized;
using System.Reflection;
using System.IO;
using System.Xml;
using System.Windows.Forms;
using PVID_UHF_CE_Application;

namespace SampleUHFAt900
{
    public static class MobileConfiguration
    {
        public static NameValueCollection Settings;
        public static List<ContentMetaData> tagDetailsCollection = new List<ContentMetaData>();
        public static NameValueCollection GraphSettings;
        public static double MinX;
        public static double MinY;
        public static double MaxX;
        public static double MaxY;
        public static double XIntervel;
        public static double YIntervel;

        static MobileConfiguration()
        {
            try
            {


                string appPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase);
                //MessageBox.Show(appPath);
                string configFile = Path.Combine(appPath, "App.config");
                if (!File.Exists(configFile))
                {
                    throw new FileNotFoundException(string.Format("Application configuration file '{0}' not found.", configFile));
                }
                XmlDocument xmlDocument = new XmlDocument();
                xmlDocument.Load(configFile);
                XmlNodeList nodeList = xmlDocument.GetElementsByTagName("appSettings");
                Settings = new NameValueCollection();
                foreach (XmlNode node in nodeList)
                {
                    foreach (XmlNode key in node.ChildNodes)
                    {
                        Settings.Add(key.Attributes["key"].Value, key.Attributes["value"].Value);
                    }
                }

                XmlNodeList graphSettings = xmlDocument.GetElementsByTagName("GraphSettings");
                MinX = Convert.ToDouble(graphSettings[0].Attributes["MinX"].Value);
                MinY = Convert.ToDouble(graphSettings[0].Attributes["MinY"].Value);
                MaxX = Convert.ToDouble(graphSettings[0].Attributes["MaxX"].Value);
                MaxY = Convert.ToDouble(graphSettings[0].Attributes["MaxY"].Value);
                XIntervel = Convert.ToDouble(graphSettings[0].Attributes["XInterval"].Value);
                YIntervel = Convert.ToDouble(graphSettings[0].Attributes["YInterval"].Value);

                XmlNodeList tagDetailsList = xmlDocument.GetElementsByTagName("TagMetaData");

                MemoryLocations mLocation = MemoryLocations.None;
                int length;
                int startIndex;

                int values = 0;
                int valueLength = 0;
                string FormatString = string.Empty;

                foreach (XmlNode node in tagDetailsList)
                {
                    foreach (XmlNode type in node.ChildNodes)
                    {
                        length = Convert.ToInt32(type.Attributes["Length"].Value);
                        startIndex = Convert.ToInt32(type.Attributes["StartIndex"].Value);
                        if (type.Attributes.Count > 4)
                        {
                            values = Convert.ToInt32(type.Attributes["Values"].Value);
                            valueLength = Convert.ToInt32(type.Attributes["ValueLength"].Value);
                            FormatString = Convert.ToString(type.Attributes["FormatString"].Value);
                        }

                        switch (type.Attributes["MemoryLocation"].Value)
                        {
                            case "RM":
                                mLocation = MemoryLocations.RM;
                                break;
                            case "EPC":
                                mLocation = MemoryLocations.EPC;
                                break;
                            case "UM":
                                mLocation = MemoryLocations.UM;
                                break;
                        }

                        switch (type.Attributes["Type"].Value)
                        {
                            case "PVName":
                                if (length != 0)
                                    tagDetailsCollection.Add(new ContentMetaData(TagVariables.PVName, mLocation, length, startIndex));
                                break;
                            case "PVCountry":
                                if (length != 0)
                                    tagDetailsCollection.Add(new ContentMetaData(TagVariables.PVCountry, mLocation, length, startIndex));
                                break;
                            case "PVDate":
                                if (length != 0)
                                    tagDetailsCollection.Add(new ContentMetaData(TagVariables.PVDate, mLocation, length, startIndex));
                                break;
                            case "PVUSN":
                                if (length != 0)
                                    tagDetailsCollection.Add(new ContentMetaData(TagVariables.PVUSN, mLocation, length, startIndex));
                                break;
                            case "ModelNo":
                                if (length != 0)
                                    tagDetailsCollection.Add(new ContentMetaData(TagVariables.ModelNo, mLocation, length, startIndex));
                                break;
                            case "PeakWatt":
                                if (length != 0)
                                    tagDetailsCollection.Add(new ContentMetaData(TagVariables.PeakWatt, mLocation, length, startIndex));
                                break;
                            case "Im":
                                if (length != 0)
                                    tagDetailsCollection.Add(new ContentMetaData(TagVariables.Im, mLocation, length, startIndex));
                                break;
                            case "Vm":
                                if (length != 0)
                                    tagDetailsCollection.Add(new ContentMetaData(TagVariables.Vm, mLocation, length, startIndex));
                                break;
                            case "FF":
                                if (length != 0)
                                    tagDetailsCollection.Add(new ContentMetaData(TagVariables.FF, mLocation, length, startIndex));
                                break;
                            case "SCName":
                                if (length != 0)
                                    tagDetailsCollection.Add(new ContentMetaData(TagVariables.SCName, mLocation, length, startIndex));
                                break;
                            case "SCDate":
                                if (length != 0)
                                    tagDetailsCollection.Add(new ContentMetaData(TagVariables.SCDate, mLocation, length, startIndex));
                                break;
                            case "SCCountry":
                                if (length != 0)
                                    tagDetailsCollection.Add(new ContentMetaData(TagVariables.SCCountry, mLocation, length, startIndex));
                                break;
                            case "IECDate":
                                if (length != 0)
                                    tagDetailsCollection.Add(new ContentMetaData(TagVariables.IECDate, mLocation, length, startIndex));
                                break;
                            case "IECName":
                                if (length != 0)
                                    tagDetailsCollection.Add(new ContentMetaData(TagVariables.IECDate, mLocation, length, startIndex));
                                break;
                            case "Isc":
                                if (length != 0)
                                    tagDetailsCollection.Add(new ContentMetaData(TagVariables.Isc, mLocation, length, startIndex));
                                break;
                            case "Voc":
                                if (length != 0)
                                    tagDetailsCollection.Add(new ContentMetaData(TagVariables.Voc, mLocation, length, startIndex));
                                break;
                            case "iPoints":
                                if (length != 0)
                                    tagDetailsCollection.Add(new ContentMetaData(TagVariables.iPoints, mLocation, length, startIndex, values, valueLength, FormatString));
                                break;
                            case "vPoints":
                                if (length != 0)
                                    tagDetailsCollection.Add(new ContentMetaData(TagVariables.vPoints, mLocation, length, startIndex, values, valueLength, FormatString));
                                break;
                        }
                        //type.Attributes["value"].Value;
                    }
                }

            }
            catch (Exception ex)
            {
                AppLogWriter.WriteErrorLog("MC Error: " + ex.Message);
                MessageBox.Show("MC: " + ex.Message);
            }
        }
    }
}
